using System;
using System.Collections.Generic;
using System.Data;

namespace ApplicationTemplate
{
    class CreateDamInsertAPI
    {
        public static String CreateInsert(String realTableName, String modelClassName, DataSet FieldsDs)
        {
            String tableName = ApplcationTemplate.GetTableName(realTableName);
            return String.Format(
@"public int Insert{0}({1} model, DBManager dbManager)
        {{
            AppContext tac = AppContext.Current;

            string sqlString = string.Format(@""
                INSERT INTO {{0}}
                    (
{2}
                    )
                VALUES
                    (
{3}
                    );{6}"", this.{4});

            DbCommand command = GetSqlStringCommand(sqlString);

            //パラメータセット
{5}
{8}
            //SQL文を実行する
            int result = ExecuteNonQuery(command, dbManager);
{7}
            return result;
        }}
",
            tableName,
            modelClassName,
            GetInsertFields(FieldsDs),
            GetInsertFieldsParams(FieldsDs),
            realTableName,
            GetInsertFieldsBind(FieldsDs),
            GetIdentitySQL(FieldsDs),
            GetIdentityValue(FieldsDs),
            GetOut(FieldsDs));
        }

        private static String GetOut(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];

                if (row["is_identity"].ToString() == "True")
                {
                    String f = row["name"].ToString();
                    return "            AddOutParameter(command, \"@" + f + "\", DbType.Int32, int.MaxValue);";
                }

            }

            return String.Empty;

        }
        private static String GetIdentitySQL(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];

                if (row["is_identity"].ToString() == "True")
                {
                    return "SELECT @" + row["name"].ToString() + " = SCOPE_IDENTITY();";
                }

            }

            return String.Empty;
        }

        private static String GetIdentityValue(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                string sqlString = string.Empty;

                if (row["is_identity"].ToString() == "True")
                {
                    String f = row["name"].ToString();
                    if (row["user_type_id"].ToString() == "56")
                    {
                        sqlString = "            model." + f + " = Int32.Parse(GetParameterValue(command, \"@" + f + "\").ToString());";
                    }
                    else if (row["user_type_id"].ToString() == "127")
                    {
                        sqlString = "            model." + f + " = Int64.Parse(GetParameterValue(command, \"@" + f + "\").ToString());";
                    }

                    return sqlString;
                }
            }

            return String.Empty;
        }

        private static String GetInsertFields(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];

                if (row["is_identity"].ToString() == "True" || row["name"].ToString() == "RowVersion")
                {
                    continue;
                }

                if (String.IsNullOrEmpty(fields))
                {
                    fields += "                        " + row["name"].ToString();
                }
                else
                {
                    fields += Environment.NewLine + "                        ," + row["name"].ToString();
                }
            }

            return fields;
        }

        private static String GetInsertFieldsParams(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];

                if (row["is_identity"].ToString() == "True" || row["name"].ToString() == "RowVersion")
                {
                    continue;
                }

                if (String.IsNullOrEmpty(fields))
                {
                    fields += "                        @" + row["name"].ToString();
                }
                else
                {
                    fields += Environment.NewLine + "                       ,@" + row["name"].ToString();
                }
            }

            return fields;
        }

        private static String GetInsertFieldsBind(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                if (row["is_identity"].ToString() == "True" || row["name"].ToString() == "RowVersion")
                {
                    continue;
                }

                if (name == "create_by" || name == "last_person")
                {
                    fields += "            AddInParameter(command, \"@" + name + "\", DbType.String, tac.UserDataManager.UserId);" + Environment.NewLine;
                    continue;
                }

                if (name == "create_date")
                {
                    fields += "            AddInParameter(command, \"@" + name + "\", DbType.DateTime, tac.InsertTime);" + Environment.NewLine;
                    continue;
                }

                if (name == "update_date")
                {
                    fields += "            AddInParameter(command, \"@" + name + "\", DbType.DateTime, tac.UpdateTime);" + Environment.NewLine;
                    continue;
                }

                String type = "String";
                //Add by wangsw begin 20121018 beign
                if (user_type_id == "52")
                {
                    type = "Int16";
                }
                if (user_type_id == "56")
                {
                    type = "Int32";
                }
                if (user_type_id == "127")
                {
                    type = "Int64";
                }
                //Add by wangsw begin 20121018 end

                //对应58 smalldatetime
                else if (user_type_id == "61" || user_type_id == "40" || user_type_id == "58")
                {
                    type = "DateTime";
                }
                
                //Add by changqiguo 20121029
                if (user_type_id == "106")
                {
                    type = "Decimal";
                }
                if (user_type_id == "104")
                {
                    type = "Boolean";
                }

                fields += String.Format(@"            AddInParameter(command, ""@{0}"", DbType.{1}, model.{0});", name, type) + Environment.NewLine;

            }

            return fields;
        }
    }
}
